<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/8/8
 * Time: 16:20
 */

namespace App\Models\CacheModel\zl8;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class Zl8UserChatReservationRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl8_user_chat_reservation_record';

    protected $cacheModel = 'voice_consult.reservation';

    public $statusList = [
        'waiting_response' => 1,//待接受
        'wait_keep_promise' => 2,//待履约
        'keep_promise' => 3,//已履约
        'user_cancel' => 4,//用户取消
        'no_keep_promise' => 5,//用户未履约
        'astr_refuse' => 6,//咨询师拒绝
        'astr_no_response' => 7,//咨询师未处理预约
        'astr_cancel' => 8,//咨询师取消
    ];

    /**
     * 预约时间段配置
     * @var string[]
     */
    public $reservationTimeList = [
        1 => '00:00-01:00',
        2 => '01:00-02:00',
        3 => '02:00-03:00',
        4 => '03:00-04:00',
        5 => '04:00-05:00',
        6 => '05:00-06:00',
        7 => '06:00-07:00',
        8 => '07:00-08:00',
        9 => '08:00-09:00',
        10 => '09:00-10:00',
        11 => '10:00-11:00',
        12 => '11:00-12:00',
        13 => '12:00-13:00',
        14 => '13:00-14:00',
        15 => '14:00-15:00',
        16 => '15:00-16:00',
        17 => '16:00-17:00',
        18 => '17:00-18:00',
        19 => '18:00-19:00',
        20 => '19:00-20:00',
        21 => '20:00-21:00',
        22 => '21:00-22:00',
        23 => '22:00-23:00',
        24 => '23:00-24:00',
    ];

    /**
     * 获取用户预约列表
     * @param $uid
     * @return array|mixed
     * @throws \Exception
     */
    public function getUserReservationList($uid)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->ZsGetIocData('list', function () use ($uid)
        {
            try
            {
                return $this->where('uid', $uid)->column('status', 'id');
            }
            catch (\Exception $e)
            {
                \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                return [];
            }
        }, true);
    }

    /**
     * 获取咨询师预约列表
     * @param $astrId
     * @return array|mixed
     * @throws \Exception
     */
    public function getAstrReservationList($astrId)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('astr_list', $astrId)->ZsGetIocData('astr_list', function () use ($astrId)
        {
            try
            {
                return $this->where('astr_id', $astrId)->column('h_id', 'id');
            }
            catch (\Exception $e)
            {
                \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                return [];
            }
        }, true);
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('status,uid,astr_id,date,h_id,create_time,char_id', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['status', 'uid', 'astr_id', 'date', 'h_id', 'create_time', 'char_id', 'id']);
        }
        return $data;
    }

    public function saveData($data)
    {
        try
        {
            $id = $this->insertGetId([
                'status' => $data['status'],
                'char_id' => $data['char_id'],
                'uid' => $data['uid'],
                'astr_id' => $data['astr_id'],
                'date' => $data['date'],
                'h_id' => $data['h_id'],
                'create_time' => time()
            ]);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['uid'])->rmCache('list');
            CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('astr_list', $data['astr_id'])->rmCache('astr_list');
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            $id = 0;
        }
        return $id;
    }

    public function updateData($id, $data)
    {
        try
        {
            $this->where('id', $id)->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }
}
